const mongoose = require('mongoose');

const destinationSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  nameEn: {
    type: String,
    required: true,
    trim: true
  },
  country: {
    type: String,
    required: true,
    trim: true
  },
  description: {
    type: String,
    required: true
  },
  shortDescription: {
    type: String,
    maxlength: 200
  },
  image: {
    type: String,
    required: true
  },
  imageGallery: [{
    type: String
  }],
  // 地理坐标信息
  coordinates: {
    lat: {
      type: Number,
      required: true
    },
    lng: {
      type: Number,
      required: true
    }
  },
  // 地址信息
  address: {
    type: String,
    trim: true
  },
  region: {
    type: String,
    trim: true
  },
  // 评分和评价
  rating: {
    type: Number,
    min: 0,
    max: 5,
    default: 0
  },
  reviewCount: {
    type: Number,
    default: 0
  },
  // 热门程度
  popularLevel: {
    type: Number,
    min: 1,
    max: 5,
    default: 3
  },
  isPopular: {
    type: Boolean,
    default: false
  },
  // 季节和时间信息
  bestSeason: {
    type: String,
    default: '全年'
  },
  visitDuration: {
    type: String, // 如 "2-3天", "半天"
    default: '1天'
  },
  // 分类和标签
  category: {
    type: String,
    enum: ['city', 'nature', 'culture', 'beach', 'mountain', 'food'],
    required: true
  },
  tags: [{
    type: String,
    trim: true
  }],
  // 价格信息
  priceRange: {
    min: {
      type: Number,
      default: 0
    },
    max: {
      type: Number,
      default: 1000
    },
    currency: {
      type: String,
      default: 'CNY'
    }
  },
  // 关联资源
  attractions: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Attraction'
  }],
  hotels: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Hotel'
  }],
  // 交通信息
  transportation: {
    airport: String,
    publicTransport: String,
    driveTime: String
  },
  // 实用信息
  language: String,
  timezone: String,
  climate: String,
  currency: String,
  // 状态
  status: {
    type: String,
    enum: ['active', 'inactive', 'draft'],
    default: 'active'
  }
}, {
  timestamps: true
});

// 创建地理空间索引
destinationSchema.index({ coordinates: '2dsphere' });

// 创建文本搜索索引
destinationSchema.index({
  name: 'text',
  nameEn: 'text',
  description: 'text',
  tags: 'text'
});

// 虚拟字段：获取附近的目的地
destinationSchema.virtual('nearbyDestinations', {
  ref: 'Destination',
  localField: '_id',
  foreignField: 'coordinates'
});

// 实例方法：计算与其他目的地的距离
destinationSchema.methods.distanceTo = function(otherDestination) {
  const R = 6371; // 地球半径（公里）
  const dLat = (otherDestination.coordinates.lat - this.coordinates.lat) * Math.PI / 180;
  const dLng = (otherDestination.coordinates.lng - this.coordinates.lng) * Math.PI / 180;
  const a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(this.coordinates.lat * Math.PI / 180) * Math.cos(otherDestination.coordinates.lat * Math.PI / 180) * 
    Math.sin(dLng/2) * Math.sin(dLng/2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  const distance = R * c;
  return distance;
};

// 静态方法：查找附近的目的地
destinationSchema.statics.findNearby = function(coordinates, maxDistance = 50) {
  return this.find({
    coordinates: {
      $near: {
        $geometry: {
          type: 'Point',
          coordinates: [coordinates.lng, coordinates.lat]
        },
        $maxDistance: maxDistance * 1000 // 转换为米
      }
    }
  });
};

module.exports = mongoose.model('Destination', destinationSchema); 